/*
 * This file is part of NativeAccessHooks.
 *
 * Copyleft 2014 Mark Jeronimus. All Rights Reversed.
 * https://code.google.com/p/nativeaccesshooks/
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with NativeAccessHooks. If not, see <http://www.gnu.org/licenses/>.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
package examples;

import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

import com.digitalmodular.nah.HookManager;
import com.digitalmodular.nah.NativeInputEvent;
import com.digitalmodular.nah.mouse.NativeMouseButtonEvent;
import com.digitalmodular.nah.mouse.NativeMouseButtonListener;
import com.digitalmodular.nah.mouse.NativeMouseHook;
import com.digitalmodular.nah.mouse.NativeMouseMotionEvent;
import com.digitalmodular.nah.mouse.NativeMouseMotionListener;
import com.digitalmodular.nah.mouse.NativeMouseWheelEvent;
import com.digitalmodular.nah.mouse.NativeMouseWheelListener;

/**
 * Demonstrates use of the {@link NativeMouseHook}.
 *
 * @author Mark Jeronimus
 */
// date 2014-10-12
public class TestMouseHookMain extends JFrame {

	public static void main(String[] args) {
		new TestMouseHookMain();
	}

	private JTextArea	logArea	= new JTextArea();
	private MouseLogger	logger	= new MouseLogger();

	public TestMouseHookMain() {
		// Make a log window.
		super(TestMouseHookMain.class.getSimpleName());

		add(new JScrollPane(logArea));

		addWindowListener(new WindowAdapter() {
			@Override
			public void windowClosing(WindowEvent e) {
				HookManager.unregisterNativeHooksAndExit(0);
			}
		});
		setSize(840, 560);
		setVisible(true);

		logArea.setEditable(false);

		HookManager.addNativeHook(NativeMouseHook.class);
		HookManager.registerNativeHooks();
		HookManager.addNativeMouseButtonListener(logger);
		HookManager.addNativeMouseMotionListener(logger);
		HookManager.addNativeMouseWheelListener(logger);
	}

	class MouseLogger implements NativeMouseButtonListener, NativeMouseMotionListener, NativeMouseWheelListener {
		@Override
		public void nativeMouseWheelMoved(NativeMouseWheelEvent e) {
			log(e);
		}

		@Override
		public void nativeMouseMoved(NativeMouseMotionEvent e) {
			log(e);
		}

		@Override
		public void nativeMousePressed(NativeMouseButtonEvent e) {
			log(e);
		}

		@Override
		public void nativeMouseReleased(NativeMouseButtonEvent e) {
			log(e);
		}
	}

	void log(NativeInputEvent e) {
		logArea.append(e.toString() + "\n");
		logArea.setCaretPosition(logArea.getDocument().getLength() - 1);
	}
}
